home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / space / world.c < prev   
C/C++ Source or Header  |  1996-11-11  |  8KB  |  200 lines

  1. /*
  2.  * Copyright (C) 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "space.h"
  18.  
  19. typedef struct  { flot32 x,y,z,s,t ; } P5 ;
  20.  
  21. typedef struct  {
  22.    flot32  a,b,c,d,J,K,L ;
  23.    P5      *u,*v,*w ;
  24. } t_octa ;
  25.  
  26. extern t_stopwatch Counter ;
  27. static sint32 A,B,loop ;
  28. static t_octa octa[8],*o ;
  29. static P5     dt[4][65][65],*d ;
  30. static sint32 already_count = 0 ;
  31. static sint32 already[64][2] ;
  32.  
  33. /**********************************************************************
  34. *  world_mid()  -
  35. **********************************************************************/
  36. static void world_mid(sint32 x1,sint32 y1,sint32 x2,sint32 y2)
  37.  
  38. {  register sint32 xi,yi ;
  39.    register flot32 a,b,x,y,z,w ;
  40.  
  41.    xi = (x1 + x2) >> 1 ;
  42.    yi = (y1 + y2) >> 1 ;
  43.    d = &dt[loop][yi][xi] ;
  44.  
  45.    x = dt[loop][y1][x1].x + dt[loop][y2][x2].x ;
  46.    y = dt[loop][y1][x1].y + dt[loop][y2][x2].y ;
  47.    z = dt[loop][y1][x1].z + dt[loop][y2][x2].z ;
  48.    w = sqrt(x*x+y*y+z*z) ;
  49.    d->x = x/w ; d->y = y/w ; d->z = z/w ;
  50.  
  51.    o = &octa[(yi < B-xi) ? loop+4 : loop] ;
  52.  
  53.    w = -o->d / (o->a*d->x + o->b*d->y + o->c*d->z) ;
  54.    a = w * d->x ;
  55.    b = w * d->y ;
  56.  
  57.    x = ((b-o->w->y)*(o->v->x-o->w->x) - (a-o->w->x)*(o->v->y-o->w->y)) * o->J ;
  58.    y = ((b-o->u->y)*(o->w->x-o->u->x) - (a-o->u->x)*(o->w->y-o->u->y)) * o->K ;
  59.    z = ((b-o->v->y)*(o->u->x-o->v->x) - (a-o->v->x)*(o->u->y-o->v->y)) * o->L ;
  60.  
  61.    d->s = x*o->u->s + y*o->v->s + z*o->w->s ;
  62.    d->t = x*o->u->t + y*o->v->t + z*o->w->t ;
  63. }
  64.  
  65. /**********************************************************************
  66. *  generate_sphere()  -
  67. **********************************************************************/
  68. sint32 generate_sphere(sint32 level,sint32 flag,sint32 tex_size) 
  69.  
  70. {  register flot32 x,y,z,w ;
  71.    register sint32 i,j,stage,step,dstep ;
  72.  
  73.    if (level < 0) level = 0 ;
  74.    if (level > 6) level = 6 ;
  75.  
  76.    j = (level << 16) | (flag << 12) | (tex_size) ;
  77.  
  78.    for (i=0; i<already_count; i++)
  79.      if (j == already[i][0])
  80.        return(already[i][1]) ;
  81.  
  82.    already[already_count][0] = j ;
  83.  
  84.    for (B=1,A=i=0; i<level; A=B,B<<=1,i++) ;
  85.  
  86.    w = 1.0/(float)tex_size ;
  87.  
  88.    d = &dt[0][0][0]; d->x =  0; d->y = -1; d->z =  0; d->s = 1.0-w; d->t =     w;
  89.    d = &dt[0][0][B]; d->x =  0; d->y =  0; d->z = -1; d->s = 1.0-w; d->t = 0.5-w;
  90.    d = &dt[0][B][B]; d->x =  0; d->y =  1; d->z =  0; d->s = 0.5+w; d->t = 0.5-w;
  91.    d = &dt[0][B][0]; d->x =  1; d->y =  0; d->z =  0; d->s = 0.5+w; d->t =     w;
  92.    d = &dt[1][0][0]; d->x =  0; d->y = -1; d->z =  0; d->s = 1.0-w; d->t = 1.0-w;
  93.    d = &dt[1][0][B]; d->x = -1; d->y =  0; d->z =  0; d->s = 0.5+w; d->t = 1.0-w;
  94.    d = &dt[1][B][B]; d->x =  0; d->y =  1; d->z =  0; d->s = 0.5+w; d->t = 0.5+w;
  95.    d = &dt[1][B][0]; d->x =  0; d->y =  0; d->z = -1; d->s = 1.0-w; d->t = 0.5+w;
  96.    d = &dt[2][0][0]; d->x =  0; d->y = -1; d->z =  0; d->s =     w; d->t = 1.0-w;
  97.    d = &dt[2][0][B]; d->x =  0; d->y =  0; d->z =  1; d->s =     w; d->t = 0.5+w;
  98.    d = &dt[2][B][B]; d->x =  0; d->y =  1; d->z =  0; d->s = 0.5-w; d->t = 0.5+w;
  99.    d = &dt[2][B][0]; d->x = -1; d->y =  0; d->z =  0; d->s = 0.5-w; d->t = 1.0-w;
  100.    d = &dt[3][0][0]; d->x =  0; d->y = -1; d->z =  0; d->s =     w; d->t =     w;
  101.    d = &dt[3][0][B]; d->x =  1; d->y =  0; d->z =  0; d->s = 0.5-w; d->t =     w;
  102.    d = &dt[3][B][B]; d->x =  0; d->y =  1; d->z =  0; d->s = 0.5-w; d->t = 0.5-w;
  103.    d = &dt[3][B][0]; d->x =  0; d->y =  0; d->z =  1; d->s =     w; d->t = 0.5-w;
  104.  
  105.    octa[0].u = &dt[0][B][B] ; octa[0].v = &dt[0][0][B] ; octa[0].w = &dt[0][B][0] ;
  106.    octa[4].u = &dt[0][0][0] ; octa[4].v = &dt[0][B][0] ; octa[4].w = &dt[0][0][B] ;
  107.    octa[1].u = &dt[1][B][B] ; octa[1].v = &dt[1][0][B] ; octa[1].w = &dt[1][B][0] ;
  108.    octa[5].u = &dt[1][0][0] ; octa[5].v = &dt[1][B][0] ; octa[5].w = &dt[1][0][B] ;
  109.    octa[2].u = &dt[2][B][B] ; octa[2].v = &dt[2][0][B] ; octa[2].w = &dt[2][B][0] ;
  110.    octa[6].u = &dt[2][0][0] ; octa[6].v = &dt[2][B][0] ; octa[6].w = &dt[2][0][B] ;
  111.    octa[3].u = &dt[3][B][B] ; octa[3].v = &dt[3][0][B] ; octa[3].w = &dt[3][B][0] ;
  112.    octa[7].u = &dt[3][0][0] ; octa[7].v = &dt[3][B][0] ; octa[7].w = &dt[3][0][B] ;
  113.  
  114.    for (o=octa,i=0; i<8; o++,i++)  {
  115.      x = o->u->x + o->v->x + o->w->x ; 
  116.      y = o->u->y + o->v->y + o->w->y ; 
  117.      z = o->u->z + o->v->z + o->w->z ; 
  118.      w = sqrt(x*x + y*y + z*z) ;
  119.  
  120.      o->a = x / w ;
  121.      o->b = y / w ;
  122.      o->c = z / w ;
  123.      o->d = -w / 3.0 ;
  124.  
  125.      o->J = 1./((o->u->y-o->w->y)*(o->v->x-o->w->x)-(o->u->x-o->w->x)*(o->v->y-o->w->y));
  126.      o->K = 1./((o->v->y-o->u->y)*(o->w->x-o->u->x)-(o->v->x-o->u->x)*(o->w->y-o->u->y));
  127.      o->L = 1./((o->w->y-o->v->y)*(o->u->x-o->v->x)-(o->w->x-o->v->x)*(o->u->y-o->v->y));
  128.      }
  129.  
  130.    for (loop=0; loop<4; loop++)
  131.      for (step=A,dstep=B,stage=1; stage<=level; dstep=step,step>>=1,stage++)
  132.        for (i=dstep; i<=B; i+=dstep)
  133.           for (j=step; j<=B; j+=dstep)  {
  134.              world_mid(j-step,i      ,j+step,i      ) ;   /* upper */
  135.              world_mid(j-step,i-dstep,j+step,i-dstep) ;   /* lower */
  136.              world_mid(j-step,i      ,j-step,i-dstep) ;   /* left */
  137.              world_mid(j+step,i      ,j+step,i-dstep) ;   /* right */
  138.              world_mid(j-step,i      ,j+step,i-dstep) ;   /* diag */
  139.              }
  140.  
  141.    stage = glGenLists(1);
  142.    glNewList(stage,GL_COMPILE) ;
  143.  
  144.    switch (flag)  {
  145.      case FLAT_SPHERE :
  146.                for (loop=2; loop<4; loop++)
  147.                  for (j=0; j<B; j++)  {
  148.                    glBegin(GL_TRIANGLE_STRIP) ;
  149.                    for (i=0; i<=B; i++)  {
  150.                      d = &dt[loop][i][j] ;
  151.                      glVertex3fv(&d->x);
  152.                      glVertex3fv(&(d+1)->x);
  153.                      }
  154.                    glEnd() ;
  155.                    }
  156.                break ;
  157.  
  158.      case LIGT_SPHERE :
  159.                for (loop=((Counter.flags & NDPNT_FLAG)?0:2); loop<4; loop++)
  160.                  for (j=0; j<B; j++)  {
  161.                    glBegin(GL_TRIANGLE_STRIP) ;
  162.                    for (i=0; i<=B; i++)  {
  163.                      d = &dt[loop][i][j];
  164.                      glNormal3fv(&d->x);
  165.                      glVertex3fv(&d->x);
  166.                      glNormal3fv(&(d+1)->x);
  167.                      glVertex3fv(&(d+1)->x);
  168.                      }
  169.                    glEnd() ;
  170.                    }
  171.                break ;
  172.  
  173.      case TEXX_SPHERE :
  174.                for (loop=0; loop<4; loop++)
  175.                  for (j=0; j<B; j++)  {
  176.                    glBegin(GL_TRIANGLE_STRIP) ;
  177.                    for (i=0; i<=B; i++)  {
  178.                      d = &dt[loop][i][j];
  179.                      glTexCoord2fv(&d->s);
  180.                      glNormal3fv(&d->x);
  181.                      glVertex3fv(&d->x);
  182.                      glTexCoord2fv(&(d+1)->s);
  183.                      glNormal3fv(&(d+1)->x);
  184.                      glVertex3fv(&(d+1)->x);
  185.                      }
  186.                    glEnd() ;
  187.                    }
  188.                break ;
  189.  
  190.      default : printf("ERROR: generate_sphere()\n") ;
  191.                break ;
  192.      }
  193.  
  194.    glEndList();
  195.  
  196.    already[already_count++][1] = stage ; 
  197.    return(stage) ;
  198. }
  199.  
  200.